package test.wechat.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lodsve.wechat.beans.Industry;
import lodsve.wechat.beans.Template;
import lodsve.wechat.beans.TemplateData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import test.wechat.service.WeixinService;

import java.util.List;

/**
 * .
 *
 * @author sunhao(sunhao.java@gmail.com)
 * @version V1.0, 16/3/4 下午1:22
 */
@Api(value = "微信模板消息API测试", description = "微信模板消息API测试")
@RestController
@RequestMapping("/weixin/template")
public class TemplateController {
    @Autowired
    private WeixinService weixinService;

    @ApiOperation(value = "设置所属行业", notes = "设置所属行业")
    @ApiResponses({@ApiResponse(code = 200, message = "设置所属行业成功")})
    @RequestMapping(value = "/set/{industryId1}/{industryId2}", method = RequestMethod.POST, produces = "application/json")
    public void setIndustry(@ApiParam(name = "industryId1", value = "公众号模板消息所属行业编号") @PathVariable("industryId1") int industryId1,
                            @ApiParam(name = "industryId2", value = "公众号模板消息所属行业编号") @PathVariable("industryId2") int industryId2) {
        weixinService.setIndustry(industryId1, industryId2);
    }

    @ApiOperation(value = "获取设置的行业信息", notes = "获取设置的行业信息")
    @ApiResponses({@ApiResponse(code = 200, message = "获取设置的行业信息成功")})
    @RequestMapping(value = "/industry", method = RequestMethod.GET, produces = "application/json")
    public Industry getIndustry() {
        return weixinService.getIndustry();
    }

    @ApiOperation(value = "根据模板短名称获取模板", notes = "根据模板短名称获取模板")
    @ApiResponses({@ApiResponse(code = 200, message = "获取模板成功")})
    @RequestMapping(value = "/{shortId}", method = RequestMethod.POST, produces = "application/json")
    public String getTemplate(@ApiParam(name = "shortId", value = "模板短名称") @PathVariable("shortId") String shortId) {
        return weixinService.getTemplateId(shortId);
    }

    @ApiOperation(value = "获取模板列表", notes = "获取模板列表")
    @ApiResponses({@ApiResponse(code = 200, message = "获取模板列表成功")})
    @RequestMapping(value = "/list", method = RequestMethod.GET, produces = "application/json")
    public List<Template> listAllTemplates() {
        return weixinService.listAllTemplates();
    }

    @ApiOperation(value = "删除模板列表", notes = "删除模板列表")
    @ApiResponses({@ApiResponse(code = 200, message = "删除模板列表成功")})
    @RequestMapping(value = "/delete/{templateId}", method = RequestMethod.POST, produces = "application/json")
    public void deleteTemplate(@ApiParam(name = "templateId", value = "模板id") @PathVariable("templateId") String templateId) {
        weixinService.deleteTemplate(templateId);
    }

    @ApiOperation(value = "发送模板消息", notes = "发送模板消息")
    @ApiResponses({@ApiResponse(code = 200, message = "发送模板消息成功")})
    @RequestMapping(value = "/send", method = RequestMethod.POST, produces = "application/json")
    public void sendTemplate(@ApiParam(name = "toUser", value = "接受者OpenId") @RequestParam("toUser") String toUser,
                             @ApiParam(name = "templateId", value = "使用的模板Id") @RequestParam("templateId") String templateId,
                             @ApiParam(name = "url", value = "微信中点击消息后进入的页面") @RequestParam("url") String url,
                             @ApiParam(name = "datas", value = "需要的数据") @RequestBody List<TemplateData> datas) {
        weixinService.sendTemplate(toUser, templateId, url, datas);
    }
}
